#include "key.h"


/*
 *  KEY_UP      ----->  PA0   ----> pressed is gnd
 *  KEY_DOWN    ----->  PA1   ----> pressed is gnd
 *  KEY_LEFT    ----->  PB12  ----> pressed is gnd
 *  KEY_RIGHT   ----->  PB13  ----> pressed is gnd
 *  KEY_OK      ----->  PB14  ----> pressed is gnd
 *  KEY_CANCEL  ----->  PB15  ----> pressed is gnd
 *
 * */

typedef enum
{
    KEY_CHECKING  = 0,
    KEY_CONFIRM   = 1,
    KEY_RELEASE   = 2
}KEY_STATE;


#define KEY_UP_PORT       GPIOA
#define KEY_UP_PINS       GPIO_Pin_0
#define KEY_UP_PRESSED    GPIO_ReadInputDataBit(KEY_UP_PORT,KEY_UP_PINS)

#define KEY_DOWN_PORT     GPIOA
#define KEY_DOWN_PINS     GPIO_Pin_1
#define KEY_DOWN_PRESSED  GPIO_ReadInputDataBit(KEY_DOWN_PORT,KEY_DOWN_PINS)

#define KEY_LEFT_PORT      GPIOB
#define KEY_LEFT_PINS      GPIO_Pin_12
#define KEY_LEFT_PRESSED   GPIO_ReadInputDataBit(KEY_LEFT_PORT,KEY_LEFT_PINS)

#define KEY_RIGHT_PORT     GPIOB
#define KEY_RIGHT_PINS     GPIO_Pin_13
#define KEY_RIGHT_PRESSED  GPIO_ReadInputDataBit(KEY_RIGHT_PORT,KEY_RIGHT_PINS)

#define KEY_OK_PORT        GPIOB
#define KEY_OK_PINS        GPIO_Pin_14
#define KEY_OK_PRESSED     GPIO_ReadInputDataBit(KEY_OK_PORT,KEY_OK_PINS)

#define KEY_CANCEL_PORT    GPIOB
#define KEY_CANCEL_PINS    GPIO_Pin_15
#define KEY_CANCEL_PRESSED GPIO_ReadInputDataBit(KEY_CANCEL_PORT,KEY_CANCEL_PINS)

#define KEY_STATE_RELEASE (KEY_UP_PRESSED && KEY_DOWN_PRESSED && KEY_LEFT_PRESSED && KEY_RIGHT_PRESSED && KEY_OK_PRESSED && KEY_CANCEL_PRESSED)


void key_hardware_init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0 | GPIO_Pin_1;  // TX ----> PA2
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_12 | GPIO_Pin_13 |GPIO_Pin_14 | GPIO_Pin_15;  // TX ----> PA2
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}

uint8_t key_hardware_keys_poll(void)
{
    static KEY_STATE key_status = KEY_CHECKING;
    static uint8_t m_delay_up = 0;
    static uint8_t key_val = 0;
    switch(key_status) {
        case KEY_CHECKING: // no key is be pressed
            if (!KEY_STATE_RELEASE) {
                key_status = KEY_CONFIRM; //next step
            }
            break;
        case KEY_CONFIRM:
            if (KEY_UP_PRESSED == 0)      {
                key_val = 1;
                key_status = KEY_RELEASE;
            }
            else if (KEY_DOWN_PRESSED == 0)    {
                key_val = 2;
                key_status = KEY_RELEASE;
            }
            else if (KEY_LEFT_PRESSED == 0)    {
                key_val = 3;
                key_status = KEY_RELEASE;
            }
            else if (KEY_RIGHT_PRESSED == 0)   {
                key_val = 4;
                key_status = KEY_RELEASE;
            }
            else if (KEY_CANCEL_PRESSED == 0)  {
                key_val = 5;
                key_status = KEY_RELEASE;
            }
            else if (KEY_OK_PRESSED == 0)      {
                key_val = 6;
                key_status = KEY_RELEASE;
            }
            else key_status = KEY_CHECKING; //first step
            break;
        case KEY_RELEASE:
            if (KEY_STATE_RELEASE) {
                if (m_delay_up++ >= 3) {
                    key_val = 0;
                    m_delay_up = 0;
                    key_status = KEY_CHECKING;
                }
            }
            break;


    }
    return key_val;

}

